home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / ANTENNA / YAGIU112 / PERFORM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-25  |  4.3 KB  |  136 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include "yagi.h"
  6.  
  7. /* The function performance gives a performance rating for the antenna
  8. between 0 and 1, for use in a genetic algorithm to get performance. All
  9. parameters are roughly linear */ 
  10.  
  11. extern int errno;
  12. extern double Zo;
  13.  
  14. double performance(struct flags flag, struct performance_data data, struct performance_data max, struct performance_data weight, struct performance_data start)
  15. {
  16.     double a, x_err, gain,fb,swr,max_gain,product_of_weights, start_gain;
  17.     struct performance_data perform;
  18.     double max_fb,sidelobe_level,temp;
  19.     double max_sidelobe;
  20.     int items=0;
  21.     memset((char*) &perform,1,sizeof(perform));
  22.     /* perform.gain=1.0; perform.fb=1.0; perform.swr=1.0;
  23.     perform.fb=1.0; perform.r=1.0; perform.x=1.0;
  24.     perform.sidelobe=1.0; */
  25.     product_of_weights=1.0;
  26.     weight.sidelobe/=100.0;
  27.     /* To normalise the fitness, we must divide by the products of the
  28.     weights, so this must be found. */
  29.     if(weight.gain!=0.0)
  30.         product_of_weights=weight.gain;
  31.     if(weight.fb!=0.0)
  32.         product_of_weights*=weight.fb;
  33.     if(weight.r!=0.0)
  34.         product_of_weights*=weight.r;
  35.     if(weight.x!=0.0)
  36.         product_of_weights*=weight.x;
  37.     if(weight.swr!=0.0)
  38.         product_of_weights*=weight.swr;
  39.     if(weight.sidelobe!=0.0)
  40.         product_of_weights*=weight.sidelobe;
  41.     if(  ((flag.Wflg&GAIN)==GAIN) ||  ((flag.gflg&GAIN)==GAIN) )
  42.     {
  43.         /* gain=pow(10.0,data.gain/10.0);       
  44.         start_gain=pow(10.0,(start.gain-1.0)/10.0);       
  45.         max_gain=pow(10.0,max.gain/10.0);
  46.         if(gain>1.2*max_gain)
  47.             gain=max_gain;
  48.         perform.gain=weight.gain*(gain-start_gain)/(max_gain-start_gain); */
  49.         perform.gain=weight.gain*(data.gain-5)/(max.gain-5);
  50.         /* printf("perform.g= %lf g=%lf\n", perform.gain, data.gain);  */
  51.         if(perform.gain < 0)
  52.             perform.gain=0.0;
  53.         items++;
  54.     }
  55.     if(  ((flag.Wflg&FB)==FB) ||  ((flag.gflg&FB)==FB) )
  56.     {
  57.         fb=pow(10.0,data.fb/10.0);       
  58.         max_fb=pow(10.0,max.fb/10.0);
  59.         if((fb>max_fb) &(!flag.Oflg))
  60.             fb=max_fb; 
  61.         perform.fb=weight.fb*fb/max_fb; 
  62.         items++;
  63.     }
  64.     if(  ((flag.Wflg&RESISTANCE)==RESISTANCE) || ((flag.gflg&RESISTANCE)==RESISTANCE))
  65.     {
  66.         perform.r=(1-pow(fabs(Zo-data.r)/Zo,0.25));  
  67.         if(data.r<0.7*Zo || data.r > 1.4*Zo)
  68.             perform.r/=10.0;
  69.         items++;
  70.     }
  71.     if(  ((flag.Wflg&REACTANCE)==REACTANCE) || ((flag.gflg&REACTANCE)==REACTANCE))
  72.     {
  73.         x_err=fabs(data.x)/Zo;  
  74.         if(x_err > 1.0)
  75.             x_err=1.0;
  76.         perform.x=1-pow(x_err,0.28);
  77.         items++;
  78.     }
  79.     if(  ((flag.Wflg&VSWR)==VSWR) ||  ((flag.gflg&VSWR)==VSWR) )
  80.     {
  81.         swr=data.swr;
  82.         /* The follwing, fitness=(1-((swr-1)/(swr+1))^2), gives a fitness
  83.         proportional to the fraction of power radiated, assuming all 
  84.         reflected power is absorbed. Unfortunately, it gives too high a#
  85.         fitness to a poor SWR, so was abandoned */
  86.         /* perform.swr=weight.swr*(1-pow((swr-1.0)/(swr+1.0),2.0) ) ;    */
  87.  
  88.  
  89.         /* The folloing is an imperical relationship developed by me, by writing
  90.         a number of SWR's down (1.0, 1.1, 1.5, 2.0, 3.0 and infinity and 
  91.         writing down what I donsidered their fitness (1.0, 0.9, 0.8, 0.5 and 0.1)
  92.         then fitting a polynomial thru em. The fitness dont work out exactly
  93.         as above, but they seem reasonable. This has a maximum value of 
  94.         0.98220 at SWR=1.0, but I wont bother addeding the scale factor
  95.         1/.98220 = 1.0181, since this will not improve things - only slow the 
  96.         program a bit! */
  97.         /* perform.swr=2.86298/(swr*swr) - 1.88078/(swr*swr*swr); */
  98.         /* Another imperical one */
  99.         /* if(swr > 7.2925)
  100.             perform.swr=1.0/(swr);
  101.         else */
  102.             perform.swr=1.0-log10(swr); 
  103.         /* if(swr>7.76124)
  104.             perform.swr=1.0/(swr*swr);
  105.         else
  106.             perform.swr=1.0-log10(swr); */
  107.             
  108.         /* printf("swr=%lf fit = %lf\n", swr, perform.swr);  */
  109.         items++;
  110.     }
  111.     if(  ((flag.Wflg&SIDE_LOBE_LEVEL)==SIDE_LOBE_LEVEL) ||  ((flag.gflg&SIDE_LOBE_LEVEL)==SIDE_LOBE_LEVEL) )
  112.     {
  113.         /* sidelobe_level=pow(10.0,data.sidelobe/10.0);       
  114.         max_sidelobe=pow(10.0,max.sidelobe/10.0);
  115.         if((sidelobe_level>max_sidelobe) & !flag.Oflg)
  116.             sidelobe_level=max_sidelobe;  
  117.         perform.sidelobe=weight.sidelobe*sidelobe_level/max_sidelobe; */
  118.         perform.sidelobe=weight.sidelobe*data.sidelobe/max.sidelobe; 
  119.         items++;
  120.     }
  121.     /* a=(perform.gain*perform.fb*perform.r*perform.x*perform.swr*perform.sidelobe)/product_of_weights; */
  122.     a=(perform.gain+perform.fb+perform.r+perform.x+perform.swr+perform.sidelobe)/(double) items;
  123. #ifdef DEBUG
  124.     if(errno)
  125.     {
  126.         fprintf(stderr,"Errno =%d in perform.c\n", errno);
  127.         exit(1);
  128.     }
  129. #endif
  130.     return(a);
  131. }
  132.  
  133.  
  134.  
  135.  
  136.